// 定义一个函数来获取图片的宽高
export const getImageDimensions = (
  file: File | Blob
): Promise<{ width: number; height: number }> => {
  return new Promise((resolve, _reject) => {
    const reader = new FileReader()
    reader.onload = event => {
      const image = new Image()
      image.onload = () => {
        resolve({ width: image.width, height: image.height })
      }
      image.src = event.target?.result as string
    }
    reader.readAsDataURL(file)
  })
}
//获取缩小比例，
export const getImageScale = async (size: number, file: File | Blob) => {
  const { width, height } = await getImageDimensions(file)
  let scale = width > height ? size / width : size / height
  scale = scale > 1 ? 1 : scale
  return scale
}

// 定义一个函数将 Blob 转换为 File
export const blobToFile = (blob: Blob, fileName: string) => {
  return new File([blob], fileName, { type: blob.type })
}
